﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenExpressApp.MetaAttribute;
using Csla.Wpf;
using OpenExpressApp.Command;
using Csla.Core;
using System.ComponentModel;
using System.Windows;
using OpenExpressApp.Template;
using OpenExpressApp.Types;
using Csla;
using OpenExpressApp.WPF.Controls;
using System.Collections.ObjectModel;
using log4net;
using System.Collections;
using OpenExpressApp.MetaModel;
using Avalon.Windows.Controls;
using OpenExpressApp.WPF.Templete;
using OpenExpressApp.Module;
using OpenExpressApp.Module.WPF;
using System.Windows.Controls;
using Ricciolo.Controls;
using System.Reflection;
using System.Windows.Markup;
using System.Diagnostics;
using AvalonDock;
using OpenExpressApp.Module.WPF.Editors;
using Csla.OpenExpressApp;

namespace OpenExpressApp.WPF.Command
{
    [Command(CommandNames.Refresh, ImageName = "Refresh.bmp", Label = "刷新", ToolbarType = ToolbarType.Main, ToolTip = "刷新列表")]
    public class RefreshCommand : WPFListViewCommand
    {
        public override bool CanExecute(ListObjectView view)
        {
            var currentObject = view.CurrentObject as ITrackStatus;
            return currentObject == null || currentObject.IsDirty == false;
        }
        /// <summary>
        /// 如果存在查询和导航面板，则刷新查询和导航面板的下拉列表框数据
        /// </summary>
        /// <param name="view"></param>
        public override void Execute(ListObjectView view)
        {
            var controller = view.DataLoader;
            QueryObjectView queryObjectView = view.NavigateQueryView;
            if (queryObjectView == null)
            {
                queryObjectView = view.CondtionQueryView;
            }

            //刷新查询或导航面板的下拉列表框数据
            if (queryObjectView != null)
            {
                var queryObject = queryObjectView.CurrentObject as IQueryObject;
                controller.AsyncGetObject(queryObject);

                foreach (var item in queryObjectView.PropertyEditors)
                {
                    if (item.PropertyInfo.ViewInfo().EditorName == EditorNames.LookupDropDown)
                    {
                        (item as LookupListPropertyEditor).ClearDropDownData();
                    }
                }
                queryObjectView.AttachNewQueryObject();
            }
            else
            {
                view.DataLoader.AsyncGetObject();
            }
        }
    }
}
